/*
 * board.h
 *
 *  Created on: Aug 1, 2013
 *      Author: Ken Arok
 *
 * \file
 *
 * \brief Low Level Board Initialization.
 *
 * Copyright (c) 2013 PT Hanindo Automation Solutions. All rights reserved.
 *
 */

#include "config_board.h"

#if BOARD_1_0_USED

#ifndef BOARD_H_
#define BOARD_H_

#include "compiler.h"

#ifdef __cplusplus
extern "C" {
#endif


/*! \name Hardware component mounted on the board.
 */
//! @{

/* Select one of the serial flash below! */
#define ROM_PART_NAME_SST25VF080B		1
#define ROM_PART_NAME_M25P10A			0

/* EEPROM MAC Address */
#define MAC_ADDR_PART_NAME_24AA02E48	1

/* Real Time Clock */
#define RTC_PART_NAME_PCF8563			1

//! @}


#ifdef AVR_SCIF_101_H_INCLUDED
#define AVR32_SCIF_OSCCTRL0_STARTUP_2048_RCOSC	0x00000003
#define AVR32_SCIF_OSCCTRL0_STARTUP_16384_RCOSC	0x00000006
#define AVR32_SCIF_OSCCTRL32_STARTUP_8192_RCOSC	0x00000002
#endif

/*! \name Oscillator Definition
 */
//! @{

// The only Oscillator 0 is enabled by hardware.
#define FOSC0			12000000								//!< Oscillator frequency: Hz
#define OSC0_STARTUP	AVR32_SCIF_OSCCTRL0_STARTUP_2048_RCOSC	//!< Oscillator 0 startup time: RCOsc periods.

//! @}


//! SDMMC Storage Device Type
#define MICROSD		1
#define SDMMC		2

//! Select SD/MMC card type
//#define SDMMC_TYPE	SDMMC
#define SDMMC_TYPE	MICROSD

#ifndef SDMMC_TYPE
#define SDMMC_TYPE	MICROSD
#endif

/* Define this if enable SDIO support */
//#define SDIO_SUPPORT_ENABLE

#define BOARD_OSC0_HZ				12000000
#define BOARD_OSC0_STARTUP_US		2000
#define BOARD_OSC0_IS_XTAL			true

//! System Clock Bus Division Options must be changed if following value are altered
#define FSYS0_HZ					132000000UL
#define FSYS1_HZ					48000000UL
#define FCPU_HZ						66000000UL
#define FPBA_HZ						33000000UL
#define FPBB_HZ						33000000UL
#define FPBC_HZ						33000000UL


//! Timer/Counter module 0
#define TC0					(&AVR32_TC0)

//! Timer/Counter module 1
#define TC1                 (&AVR32_TC1)

//! Buzzer channel
#define BUZZER_CHANNEL		0

//! TFT BLON channel
#define TFT_BLON_CHANNEL	2

//! Heart beat channel
#define HB_CHANNEL			1


//! Heart Beat LED (LED2) Definitions
#define LED_HB_PIN			AVR32_TC1_A1_0_1_PIN
#define LED_HB_FUNCTION		AVR32_TC1_A1_0_1_FUNCTION


//! Buzzer Definitions
#define BUZZER_PIN			AVR32_TC0_B0_0_1_PIN
#define BUZZER_FUNCTION		AVR32_TC0_B0_0_1_FUNCTION

//! GPIO Connection of Signal LED (LED1)
#define LED1_GPIO	AVR32_PIN_PB03

/*! \name SDRAM Definition
 */
//! @{
// Define this part if using SDRAM AS4C16M16S-7TCN from Alliance
#define SDRAM_PART_HDR	"as4c16m16s7tcn.h"

// Heap size 32MB
#define TOTAL_HEAP_SIZE		0x02000000

// Data bus width to use the SDRAM
#define SDRAM_DBW	16

//! @}


/* \name USB Definitions
 */
//! @{

/* Multiplexed pin used for USB_ID: AVR32_USBC_ID_x.
To be selected according to the AVR32_USBC_ID_x_PIN and AVR32_USBC_USB_ID_FUNCTION definitions from <avr32/uc3cxxxx.h */
#define USB_ID		AVR32_USBC_ID_

/* Multiplexed pin used for USB_VBOF: AVR32_USBC_VBOF_x.
To be selected according to the AVR32_USBC_VBOF_x_PIN and AVR32_USBC_USB_VBOF_FUNCTION definitions from <avr32/uc3cxxxx.h */
#define USB_VBOF	AVR32_USBC_VBOF

//! Active level of USB_VBOF output pin.
#define USB_VBOF_ACTIVE_LEVEL		LOW

//! USB over current detection pin.
#define USB_OVERCURRENT_DETECT_PIN	AVR32_PIN_PB18

//! @}

/*! \name SPI0 Definitions
 */
//! @{
#define DEFAULT_SPI0				(&AVR32_SPI0)
#define DEFAULT_SPI0_SCK_PIN		AVR32_SPI0_SCK_0_2_PIN
#define DEFAULT_SPI0_SCK_FUNCTION	AVR32_SPI0_SCK_0_2_FUNCTION
#define DEFAULT_SPI0_MOSI_PIN		AVR32_SPI0_MOSI_0_2_PIN
#define DEFAULT_SPI0_MOSI_FUNCTION	AVR32_SPI0_MOSI_0_2_FUNCTION
#define DEFAULT_SPI0_MISO_PIN		AVR32_SPI0_MISO_0_2_PIN
#define DEFAULT_SPI0_MISO_FUNCTION	AVR32_SPI0_MISO_0_2_FUNCTION
//! @}


/*! \name SPI1 Definitions
 */
//! @{
#define DEFAULT_SPI1				(&AVR32_SPI1)
#define DEFAULT_SPI1_SCK_PIN		AVR32_SPI1_SCK_0_1_PIN
#define DEFAULT_SPI1_SCK_FUNCTION	AVR32_SPI1_SCK_0_1_FUNCTION
#define DEFAULT_SPI1_MOSI_PIN		AVR32_SPI1_MOSI_0_1_PIN
#define DEFAULT_SPI1_MOSI_FUNCTION	AVR32_SPI1_MOSI_0_1_FUNCTION
#define DEFAULT_SPI1_MISO_PIN		AVR32_SPI1_MISO_0_1_PIN
#define DEFAULT_SPI1_MISO_FUNCTION	AVR32_SPI1_MISO_0_1_FUNCTION
//! @}


/*! \name TWI/I2C Definitions
 */
//! @{
#define DEFAULT_TWIM0					(&AVR32_TWIM0)
#define DEFAULT_TWIMS0_TWCK_PIN			AVR32_TWIMS0_TWCK_0_0_PIN
#define DEFAULT_TWIMS0_TWCK_FUNCTION	AVR32_TWIMS0_TWCK_0_0_FUNCTION
#define DEFAULT_TWIMS0_TWD_PIN			AVR32_TWIMS0_TWD_0_0_PIN
#define DEFAULT_TWIMS0_TWD_FUNCTION		AVR32_TWIMS0_TWD_0_0_FUNCTION
#define DEFAULT_TWIMS0_TWALM_PIN		AVR32_TWIMS0_TWALM_PIN
#define DEFAULT_TWIMS0_TWALM_FUNCTION	AVR32_TWIMS0_TWALM_FUNCTION
//! @}


/*! \name GPIO Connections of LCD Character and LCD Graphic Monochrome (Address: CN10)
 */
//! @{
#define LCD_SPI					DEFAULT_SPI0
#define LCD_SPI_NPCS			0
#define LCD_SPI_NPCS_PIN		AVR32_SPI0_NPCS_0_3_PIN
#define LCD_SPI_NPCS_FUNCTION	AVR32_SPI0_NPCS_0_3_FUNCTION
#define LCD_SPI_SCK_PIN			DEFAULT_SPI0_SCK_PIN
#define LCD_SPI_SCK_FUNCTION	DEFAULT_SPI0_SCK_FUNCTION
#define LCD_SPI_MOSI_PIN		DEFAULT_SPI0_MOSI_PIN
#define LCD_SPI_MOSI_FUNCTION	DEFAULT_SPI0_MOSI_FUNCTION
#define LCD_E_GPIO				AVR32_PIN_PA20
#define LCD_BLON_GPIO			AVR32_PIN_PA21
#define LCD_WR_RW_GPIO			AVR32_PIN_PA22
#define LCD_A0_RS_GPIO			AVR32_PIN_PA23
#define LCD_CS_GPIO				AVR32_PIN_PA24
#define LCD_RESET_GPIO			AVR32_PIN_PA25
//! @}

/* ! \name GPIO Connections of LCD TFT (Address: CN11)
 */
//! @{
#define TFT_BLON_PIN			AVR32_TC0_A2_0_1_PIN
#define TFT_BLON_FUNCTION		AVR32_TC0_A2_0_1_FUNCTION
#define TFT_NCS_GPIO			AVR32_PIN_PD28
#define TFT_RD_GPIO				AVR32_PIN_PD26
#define TFT_WR_GPIO				AVR32_PIN_PD25
#define TFT_DNC_GPIO			AVR32_PIN_PB01
#define TFT_RESET_GPIO			AVR32_PIN_PC13
//! @}


/*! \name GPIO Connections of KEYPAD (Address: CN14)
 */
//! @{
#define KEY_SPI						DEFAULT_SPI0
#define KEY_SPI_NPCS_WR				1
#define KEY_SPI_NPCS_WR_PIN			AVR32_SPI0_NPCS_1_3_PIN
#define KEY_SPI_NPCS_WR_FUNCTION	AVR32_SPI0_NPCS_1_3_FUNCTION
#define KEY_SPI_NPCS_RD				2
#define KEY_SPI_NPCS_RD_PIN			AVR32_SPI0_NPCS_2_2_PIN
#define KEY_SPI_NPCS_RD_FUNCTION	AVR32_SPI0_NPCS_2_2_FUNCTION
#define KEY_SPI_SCK_PIN				DEFAULT_SPI0_SCK_PIN
#define KEY_SPI_SCK_FUNCTION		DEFAULT_SPI0_SCK_FUNCTION
#define KEY_SPI_MOSI_PIN			DEFAULT_SPI0_MOSI_PIN
#define KEY_SPI_MOSI_FUNCTION		DEFAULT_SPI0_MOSI_FUNCTION
#define KEY_SPI_MISO_PIN			DEFAULT_SPI0_MISO_PIN
#define KEY_SPI_MISO_FUNCTION		DEFAULT_SPI0_MISO_FUNCTION
#define KEY_LOAD_GPIO				AVR32_PIN_PA19
#define KEY_INT_PIN					AVR32_EIC_EXTINT_3_0_PIN
#define KEY_INT_FUNCTION			AVR32_EIC_EXTINT_3_0_FUNCTION
//! @}

/*! \name SPI Connections of DataFlash memory or EEPROM (Address: IC4 on sheet1)
 */
//! @{
#define DATAFLASH_SPI					DEFAULT_SPI1
#define DATAFLASH_SPI_NPCS				2
#define DATAFLASH_SPI_NPCS_PIN			AVR32_SPI1_NPCS_2_2_PIN
#define DATAFLASH_SPI_NPCS_FUNCTION		AVR32_SPI1_NPCS_2_2_FUNCTION
#define DATAFLASH_SPI_SCK_PIN			DEFAULT_SPI1_SCK_PIN
#define DATAFLASH_SPI_SCK_FUNCTION		DEFAULT_SPI1_SCK_FUNCTION
#define DATAFLASH_SPI_MISO_PIN			DEFAULT_SPI1_MISO_PIN
#define DATAFLASH_SPI_MISO_FUNCTION		DEFAULT_SPI1_MISO_FUNCTION
#define DATAFLASH_SPI_MOSI_PIN			DEFAULT_SPI1_MOSI_PIN
#define DATAFLASH_SPI_MOSI_FUNCTION		DEFAULT_SPI1_MOSI_FUNCTION
//! @}

/*! \name GPIO and SPI Connections of SD/MMC Connector (Address: CN8 on sheet1)
 */
//! @{
#define SD_MMC_SPI_MEM_CNT			1
#define SD_MMC_SPI_NPCS				3

#if (SDMMC_TYPE == SDMMC)
#define SD_MMC_0_CD_GPIO			AVR32_PIN_PA29
#define SD_MMC_0_CD_DETECT_VALUE	1
#define SD_MMC_0_WP_GPIO			AVR32_PIN_PA27
#define SD_MMC_0_WP_DETECT_VALUE	0
#endif
//! @}

#if (SDMMC_TYPE == SDMMC)
#define SD_MMC_CARD_DETECT_PIN		SD_MMC_0_CD_GPIO
#define SD_MMC_WRITE_PROTECT_PIN	SD_MMC_0_WP_GPIO
#endif

#define SD_MMC_SPI					DEFAULT_SPI1
#define SD_MMC_SPI_SCK_PIN			DEFAULT_SPI1_SCK_PIN
#define SD_MMC_SPI_SCK_FUNCTION		DEFAULT_SPI1_SCK_FUNCTION
#define SD_MMC_SPI_MISO_PIN			DEFAULT_SPI1_MISO_PIN
#define SD_MMC_SPI_MISO_FUNCTION	DEFAULT_SPI1_MISO_FUNCTION
#define SD_MMC_SPI_MOSI_PIN			DEFAULT_SPI1_MOSI_PIN
#define SD_MMC_SPI_MOSI_FUNCTION	DEFAULT_SPI1_MOSI_FUNCTION
#define SD_MMC_SPI_NPCS_PIN			AVR32_SPI1_NPCS_3_2_PIN
#define SD_MMC_SPI_NPCS_FUNCTION	AVR32_SPI1_NPCS_3_2_FUNCTION
#define SD_MMC_WP_GPIO				AVR32_PIN_PA27
#define SD_MMC_CD_GPIO				AVR32_PIN_PA29


/*! \name USART Connections to iSmartUSART Board (Address: CN1)
 */
//! @{

// USART1: Full Duplex (Transmit and Receive)
#define USART1					(&AVR32_USART1)
#define USART1_RXD_PIN			AVR32_USART1_RXD_0_0_PIN
#define USART1_RXD_FUNCTION		AVR32_USART1_RXD_0_0_FUNCTION
#define USART1_TXD_PIN			AVR32_USART1_TXD_0_0_PIN
#define USART1_TXD_FUNCTION		AVR32_USART1_TXD_0_0_FUNCTION
#define USART1_IRQ				AVR32_USART1_IRQ
#define USART1_IRQ_GROUP		AVR32_USART1_IRQ_GROUP
#define USART1_SYSCLK			SYSCLK_USART1

// USART2: Full Duplex (Transmit and Receive) with RTS handshaking
#define USART2					(&AVR32_USART2)
#define USART2_RXD_PIN			AVR32_USART2_RXD_0_1_PIN
#define USART2_RXD_FUNCTION		AVR32_USART2_RXD_0_1_FUNCTION
#define USART2_TXD_PIN			AVR32_USART2_TXD_0_1_PIN
#define USART2_TXD_FUNCTION		AVR32_USART2_TXD_0_1_FUNCTION
#define USART2_RTS_PIN			AVR32_USART2_RTS_0_1_PIN
#define USART2_RTS_FUNCTION		AVR32_USART2_RTS_0_1_FUNCTION
#define USART2_IRQ				AVR32_USART2_IRQ
#define USART2_IRQ_GROUP		AVR32_USART2_GROUP
#define USART2_SYSCLK			SYSCLK_USART2

// USART3: Half Duplex (Receive only)
#define USART3					(&AVR32_USART3)
#define USART3_RXD_PIN			AVR32_USART3_RXD_0_0_PIN
#define USART3_RXD_FUNCTION		AVR32_USART3_RXD_0_0_FUNCTION
#define USART3_IRQ				AVR32_USART3_IRQ
#define USART3_IRQ_GROUP		AVR32_USART3_GROUP
#define USART3_SYSCLK			SYSCLK_USART3


// USART4: Full Duplex (Transmit and Receive)
#define USART4					(&AVR32_USART4)
#define USART4_RXD_PIN			AVR32_USART4_RXD_PIN
#define USART4_RXD_FUNCTION		AVR32_USART4_RXD_FUNCTION
#define USART4_TXD_PIN			AVR32_USART4_TXD_PIN
#define USART4_TXD_FUNCTION		AVR32_USART4_TXD_FUNCTION
#define USART4_IRQ				AVR32_USART4_IRQ
#define USART4_IRQ_GROUP		AVR32_USART4_GROUP
#define USART4_SYSCLK			SYSCLK_USART4

// aWire UART: Full Duplex (Transmit and Receive)
// Format: 1 start bit; 8 data bits; no parity bits; 1 stop bit
#define AWIRE					(&AVR32_AW)
#define AWIRE_TXD_PIN			AVR32_AW_DATAOUT_PIN

// SPI for USART
#define USART_SPI					DEFAULT_SPI1

// USART CONTROL GPIO
#define USART_CTRL_PIN_GPIO			AVR32_PIN_PA02

// SPI NPCS for USART
#define USART_SPI_NPCS_PIN			AVR32_SPI1_NPCS_1_2_PIN
#define USART_SPI_NPCS_FUNCTION		AVR32_SPI1_NPCS_1_2_FUNCTION
#define UART_SPI_NPCS				1

//! @}

/*! \name MACB connections to the DP83848 external PHY controller
 */
//! @{

// GPIO connection of MAC RESET signal for the external PHY controller
#define MACB_RESET_PIN					AVR32_PIN_PB05
#define EXTPHY_MACB_RESET_PIN			MACB_RESET_PIN

// GPIO connection of MAC PHY PWR_DOWN/INT signal for the external PHY controller
#define MACB_INTERRUPT_PIN				AVR32_PIN_PB00
#define EXTPHY_MACB_INTERRUPT_PIN		MACB_INTERRUPT_PIN

#define EXTPHY_MACB						(&AVR32_MACB)
#define EXTPHY_MACB_MDC_PIN				AVR32_MACB_MDC_PIN
#define EXTPHY_MACB_MDC_FUNCTION		AVR32_MACB_MDC_FUNCTION
#define EXTPHY_MACB_MDIO_PIN			AVR32_MACB_MDIO_PIN
#define EXTPHY_MACB_MDIO_FUNCTION		AVR32_MACB_MDC_FUNCTION
#define EXTPHY_MACB_RXD_0_PIN			AVR32_MACB_RXD_0_PIN
#define EXTPHY_MACB_RXD_0_FUNCTION		AVR32_MACB_RXD_0_FUNCTION
#define EXTPHY_MACB_RXD_1_PIN			AVR32_MACB_RXD_1_PIN
#define EXTPHY_MACB_RXD_1_FUNCTION		AVR32_MACB_RXD_1_FUNCTION
#define EXTPHY_MACB_TXD_0_PIN			AVR32_MACB_TXD_0_PIN
#define EXTPHY_MACB_TXD_0_FUNCTION		AVR32_MACB_TXD_0_FUNCTION
#define EXTPHY_MACB_TXD_1_PIN			AVR32_MACB_TXD_1_PIN
#define EXTPHY_MACB_TXD_1_FUNCTION		AVR32_MACB_TXD_1_FUNCTION
#define EXTPHY_MACB_TX_EN_PIN			AVR32_MACB_TX_EN_1_PIN
#define EXTPHY_MACB_TX_EN_FUNCTION		AVR32_MACB_TX_EN_1_FUNCTION
#define EXTPHY_MACB_RX_ER_PIN			AVR32_MACB_RX_ER_PIN
#define EXTPHY_MACB_RX_ER_FUNCTION		AVR32_MACB_RX_ER_FUNCTION
#define EXTPHY_MACB_RX_DV_PIN			AVR32_MACB_RX_DV_PIN
#define EXTPHY_MACB_RX_DV_FUNCTION		AVR32_MACB_RX_DV_FUNCTION
#define EXTPHY_MACB_TX_CLK_PIN			AVR32_MACB_TX_CLK_PIN
#define EXTPHY_MACB_TX_CLK_FUNCTION		AVR32_MACB_TX_CLK_FUNCTION

//! PHY Address
#define EXTPHY_PHY_ADDR					0x01

//! @}


/*! \name External SPI1 Connection (Address: CN6)
 */
//! @{
#define EXT_SPI_NPCS_PIN		AVR32_SPI1_NPCS_0_2_PIN
#define EXT_SPI_NPCS_FUNCTION	AVR32_SPI1_NPCS_0_2_FUNCTION
#define EXT_SPI_NPCS			0
//! @}



/*! \name External I/O Connection (Address: CN6)
 */
//! @{
#define EXT_IO1_GPIO		AVR32_PIN_PA06
#define EXT_IO2_GPIO		AVR32_PIN_PA07
#define EXT_IO3_GPIO		AVR32_PIN_PA08
#define EXT_IO4_GPIO		AVR32_PIN_PA09
#define EXT_IO5_GPIO		AVR32_PIN_PA10
#define EXT_IO6_GPIO		AVR32_PIN_PA11
#define EXT_IO7_GPIO		AVR32_PIN_PA12
#define EXT_IO8_GPIO		AVR32_PIN_PA13
#define EXT_IO9_GPIO		AVR32_PIN_PA14
#define EXT_IO10_GPIO		AVR32_PIN_PA15
#define EXT_IO11_GPIO		AVR32_PIN_PA16
//! @}


/*! \name JTAG Connection (Address: CN17)
 */
//! @{
#define TCK_PIN_GPIO		AVR32_PIN_PA00
#define TDI_PIN_GPIO		AVR32_PIN_PA01
#define TDO_PIN_GPIO		AVR32_PIN_PA02
#define TMS_PIN_GPIO		AVR32_PIN_PA03
//! @}



/*! \name Unused Connection
 */
//! @{
#define UNUSED_PIN1_GPIO	AVR32_PIN_PB26
#define UNUSED_PIN2_GPIO	AVR32_PIN_PC10
#define UNUSED_PIN3_GPIO	AVR32_PIN_PD04
#define UNUSED_PIN4_GPIO	AVR32_PIN_PD23
//! @}


#ifdef __AVR32_ABI_COMPILER__ // Automatically defined when compiling for AVR32, not when assembling.

/*! \brief This function initializes the board target resources.
 */
extern void board_init(void);

#endif  // #ifdef __AVR32_ABI_COMPILER__


#ifdef __cplusplus
}
#endif

#endif /* BOARD_H_ */

#endif /* BOARD_1_0_USED */
